
using AutoMapper;
using DTO;
using Microsoft.AspNetCore.Mvc;
using Model;
using SqlSugar;
using Util;

namespace API.Controllers
{
    public class MaintenanceController : BaseController
    {
        Repository<tMaintenance> _Maintenance;
        Repository<tMaintenanceApply> _Apply;
        readonly IMapper _mapper;
        public MaintenanceController(Repository<tMaintenance> Maintenance, IMapper mapper, Repository<tMaintenanceApply> apply)
        {
            _Maintenance = Maintenance;
            _mapper = mapper;
            _Apply = apply;
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultMsg<string>> Save([FromBody] SaveMaintenanceInput dto)
        {
            ResultMsg<string> result = new ResultMsg<string>();
            try
            {
                var model = _mapper.Map<tMaintenance>(dto);
                InitEntity(model);
                await _Maintenance.InsertOrUpdateAsync(model);

                var applyModel=await _Apply.GetFirstAsync(x => x.Id == model.FMId);
                if(applyModel != null)
                {
                    applyModel.FStatus = 3;
                    await _Apply.UpdateAsync(applyModel);
                }

                return result.Success();
            }
            catch (Exception ex)
            {
                Util.NLOG.LogHelper.Error("MaintenanceController--Save:" + ex.ToString());
                return result.Error(ex.ToString());
            }
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultMsg<List<MaintenanceOutput>>> GetList([FromBody] MaintenanceListInput dto)
        {
            ResultMsg<List<MaintenanceOutput>> result = new ResultMsg<List<MaintenanceOutput>>();
            try
            {
                if (dto.PageNum <= 0)
                {
                    return result.Error("PageNum不能小于或等于0");
                }
                if (dto.PageSize <= 0)
                {
                    return result.Error("PageSize不能小于或等于0");
                }
                SqlSugar.PageModel pageModel = new SqlSugar.PageModel
                {
                    PageIndex = dto.PageNum,
                    PageSize = dto.PageSize,
                };
                var exp = Expressionable.Create<tMaintenance>();
                //if (!string.IsNullOrWhiteSpace(dto.Name))
                //{
                //    exp.And(x => x.FName.Contains(dto.Name));
                //}
                var list = (await _Maintenance.GetPageListAsync(exp.ToExpression(), pageModel)).OrderByDescending(x => x.CreateTime).ToList();
                var MaintenanceOutputs = _mapper.Map<List<MaintenanceOutput>>(list);
                return result.Success(MaintenanceOutputs, pageModel.TotalCount);
            }
            catch (Exception ex)
            {
                Util.NLOG.LogHelper.Error("MaintenanceController--GetList:" + ex.ToString());
                return result.Error(ex.ToString());
            }
        }
        /// <summary>
        /// 获取唯一
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultMsg<MaintenanceOutput>> GetTheOne(string id)
        {
            ResultMsg<MaintenanceOutput> result = new ResultMsg<MaintenanceOutput>();
            try
            {
                var model = await _Maintenance.GetByIdAsync(id);
                var dao = _mapper.Map<MaintenanceOutput>(model);
                return result.Success(dao);
            }
            catch (Exception ex)
            {
                Util.NLOG.LogHelper.Error("MaintenanceController--GetTheOne:" + ex.ToString());
                return result.Error(ex.ToString());
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultMsg<string>> Del(string id)
        {
            ResultMsg<string> result = new ResultMsg<string>();
            try
            {
                await _Maintenance.DeleteByIdAsync(id);
                return result.Success();
            }
            catch (Exception ex)
            {
                Util.NLOG.LogHelper.Error("MaintenanceController--Del:" + ex.ToString());
                return result.Error(ex.ToString());
            }
        }
    }
}
